package com.jy.novel.controller.system;

import com.jy.novel.common.CommonResult;
import com.jy.novel.common.Page;
import com.jy.novel.controller.base.BaseController;
import com.jy.novel.dao.system.UserDao;
import com.jy.novel.pojo.form.UserEditForm;
import com.jy.novel.pojo.form.UserQueryForm;
import com.jy.novel.pojo.system.UserInfo;
import com.jy.novel.service.system.RoleService;
import com.jy.novel.service.system.UserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by yangqy on 4/29/16.
 */
@Controller
public class UserController extends BaseController {


    @Autowired
    UserService userService;

    @Autowired
    RoleService roleService;

    @RequestMapping(value = {"/system/user/manager"}, method = {RequestMethod.GET})
    public String manager() {
        return "system/user_manager";
    }


    @RequestMapping(
            value = {"/system/user/list"},
            method = {RequestMethod.GET}
    )
    @ResponseBody
    public Page list(
            UserQueryForm form,
            @RequestParam(value = "loginName", required = false) String loginName,
            @RequestParam(value = "roleId", required = false) String roleId,
            @RequestParam(value = "isLock", required = false) String isLock,
            @RequestParam(value = "start", required = false) String start,
            @RequestParam(value = "limit", required = false) String limit,
            @RequestParam(value = "curPage", required = false, defaultValue = "1") String curPage,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") String pageSize) {
        if (start != null) {
            curPage = String.valueOf(NumberUtils.toInt(start, 0) / NumberUtils.toInt(limit, 10) + 1);
            pageSize = limit;
        }

        if (StringUtils.isNotBlank(loginName)){
            form.setLoginName(loginName);
        }
        if (StringUtils.isNotBlank(roleId)){
            form.setRoleId(Integer.parseInt(roleId));
        }
        if (StringUtils.isNotBlank(isLock)){
            form.setIsLock(Integer.parseInt(isLock));
        }

        Page page = this.userService.findUserPage(form, NumberUtils.toInt(curPage, 1), NumberUtils.toInt(pageSize, 10));
        return page;
    }

    @RequestMapping(
            value = {"/system/user/details"},
            method = {RequestMethod.GET}
    )
    @ResponseBody
    public UserInfo details(@RequestParam(
            value = "loginName", required = true) String userName) {
        UserInfo userInfo = this.userService.getUserInfo(userName);
        return userInfo;
    }

    @RequestMapping(
            value = {"/system/user/status/change"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public CommonResult isLockChange(
            @RequestParam(value = "loginName") String loginName,
            @RequestParam(value = "isLock") String isLock) {
        if (StringUtils.isBlank(loginName)) {
            return CommonResult.returnFailure("请输入用户名", new Object[0]);
        }
        if (StringUtils.isBlank(isLock)) {
            return this.returnFailure("请输入isLock参数", new Object[0]);
        }
        boolean bool;
        switch (NumberUtils.toInt(isLock, -1)) {
            case 0:
            case 1:
                bool = this.userService.changeUserIsLock(loginName,Integer.parseInt(isLock));
                break;
            default:
                return this.returnFailure("isLock参数错误", new Object[0]);
        }
        return this.returnBoolWrap(bool, "用户不存在");
    }

    @RequestMapping(value = {"/system/user/add"}, method = {RequestMethod.POST})
    @ResponseBody
    public CommonResult userAdd(
            @RequestParam(value = "loginName") String userName,
            @RequestParam(value = "nickName") String nickName,
            @RequestParam(value = "pwd1") String pwd1,
            @RequestParam(value = "pwd2") String pwd2,
            @RequestParam(value = "roleId", required = false) String roleId) {
        if (StringUtils.isBlank(userName)) {
            return this.returnFailure("请输入账号", new Object[0]);
        } else if (StringUtils.isBlank(nickName)) {
            return this.returnFailure("请输入用户名", new Object[0]);
        } else if (StringUtils.isBlank(pwd1)) {
            return this.returnFailure("请输入密码", new Object[0]);
        } else if (StringUtils.isBlank(pwd2)) {
            return this.returnFailure("请输入确认密码", new Object[0]);
        } else if (!pwd1.equals(pwd2)) {
            return this.returnFailure("两次密码输入不一致", new Object[0]);
        }  else if (this.userService.existUserInfo(userName)) {
            return this.returnFailure("账号已存在", new Object[0]);
        } else {
            UserInfo userInfo = new UserInfo();
            userInfo.setLoginName(userName);
            userInfo.setNickName(nickName);
            userInfo.setLoginPass(pwd1);
//            userInfo.setRoleId(Integer.valueOf(NumberUtils.toInt(roleId)));
            boolean bool = this.userService.addUserInfo(userInfo);
            return this.returnBoolWrap(bool, "服务器异常");
        }
    }

    @RequestMapping(
            value = {"/system/user/edit"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public CommonResult userEdit(
            UserEditForm form,
            @RequestParam(value = "loginName", required = true) String loginName,
            @RequestParam(value = "nickName", required = true) String nickName,
            @RequestParam(value = "roleId", required = false) String roleId) {
        if (StringUtils.isBlank(loginName)) {
            return this.returnFailure("请输入账号", new Object[0]);
        }
        if (StringUtils.isBlank(nickName)){
            return this.returnFailure("请输入昵称", new Object[0]);
        }
        form.setLoginName(loginName);
        form.setNickName(nickName);
        if (StringUtils.isNotBlank(roleId)){
            form.setRoleId(Integer.valueOf(NumberUtils.toInt(roleId)));
        }
        boolean bool = this.userService.updateUserInfo(form);
        return this.returnBoolWrap(bool, "账号不存在");

    }

    @RequestMapping(
            value = {"/system/user/resetpwd"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public CommonResult userResetPwd(
            @RequestParam(value = "loginName") String loginName,
            @RequestParam(value = "pwd1") String pwd1,
            @RequestParam(value = "pwd2") String pwd2) {
        if (StringUtils.isBlank(loginName)) {
            return this.returnFailure("请输入账号", new Object[0]);
        }
        if (StringUtils.isBlank(pwd1)) {
            return this.returnFailure("请输入密码", new Object[0]);
        }
        if (StringUtils.isBlank(pwd2)) {
            return this.returnFailure("请输入确认密码", new Object[0]);
        }
        if (!pwd1.equals(pwd2)) {
            return this.returnFailure("两次密码输入不一致", new Object[0]);
        }
        boolean bool = this.userService.modifyUserPwd(loginName, pwd1);
        return this.returnBoolWrap(bool, "账号不存在");
    }

    @RequestMapping(
            value = {"/system/user/modifypwd"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public CommonResult userModifyPwd(
            @RequestParam(value = "oldPwd", required = true) String oldPwd,
            @RequestParam(value = "pwd1", required = true) String pwd1,
            @RequestParam(value = "pwd2", required = true
    ) String pwd2, HttpSession session) {
        if (StringUtils.isBlank(oldPwd)) {
            return this.returnFailure("请输入原密码", new Object[0]);
        } else if (StringUtils.isBlank(pwd1)) {
            return this.returnFailure("请输入新密码", new Object[0]);
        } else if (StringUtils.isBlank(pwd2)) {
            return this.returnFailure("请输入确认密码", new Object[0]);
        } else if (!pwd1.equals(pwd2)) {
            return this.returnFailure("两次密码输入不一致", new Object[0]);
        } else {
            UserInfo userInfo = this.getUserInfo(session);
            oldPwd = DigestUtils.md5Hex(oldPwd);
            if (!oldPwd.equalsIgnoreCase(userInfo.getLoginPass())) {
                return this.returnFailure("原密码输入错误", new Object[0]);
            } else {
                boolean bool = this.userService.modifyUserPwd(userInfo.getLoginName(), pwd1);
                if (bool) {
                    pwd1 = DigestUtils.md5Hex(pwd1);
                    userInfo.setLoginPass(pwd1);
                }

                return returnBoolWrap(bool, "账号不存在");
            }
        }
    }

}
